package com.ruitu.mall_98_2.mvp.presenter;

import com.beanu.arad.utils.Logg;
import com.beanu.arad.utils.ToastUtils;
import com.ruitu.mall_98_2.mvp.contract.CartContract;
import com.ruitu.router_module.bean.api.BaseResult;
import com.ruitu.router_module.bean.api.SubscriberImpl;
import com.ruitu.router_module.bean.cart.CartBean;

public class CartPresenter extends CartContract.Presenter {
    @Override
    public void getCarts(String accountId) {
        mModel.getCarts(accountId).subscribe(new SubscriberImpl<BaseResult<CartBean>>() {
            @Override
            public void onError(Throwable e) {
                mView.hideProgress();
                mView.setFaiel();
                ToastUtils.showShort("获取购物车失败");
                Logg.i(e.toString());
            }

            @Override
            public void onNext(BaseResult<CartBean> listBaseResult) {
                mView.hideProgress();
                if (listBaseResult.isSuccess()) {
                    mView.setData(listBaseResult.getData());
                } else {
                    mView.setFaiel();
                    ToastUtils.showShort(listBaseResult.getMsg());
                }
            }
        });
    }

    @Override
    public void deleteByIds(String ids) {
        mView.hideProgress();
        mModel.deleteByIds(ids)
                .subscribe(new SubscriberImpl<BaseResult>() {
                    @Override
                    public void onError(Throwable e) {
                        ToastUtils.showShort("删除失败");
                    }

                    @Override
                    public void onNext(BaseResult baseResult) {
                        if (baseResult.isSuccess()) {
                            mView.setDeleteSuc();
                            ToastUtils.showShort(baseResult.getMsg());
                        } else {
                            ToastUtils.showShort(baseResult.getMsg());
                        }
                    }
                });
    }

    @Override
    public void updateCartNum(String id, int num) {
        mModel.updateCartNum(id, num)
                .subscribe(new SubscriberImpl<BaseResult>() {
                    @Override
                    public void onError(Throwable e) {
                        mView.hideProgress();
                        ToastUtils.showShort("修改数量失败");
                    }

                    @Override
                    public void onNext(BaseResult baseResult) {
                        mView.hideProgress();
                        if (baseResult.isSuccess()) {
                            mView.updateCartNumSuc();
                        } else {
                            ToastUtils.showShort(baseResult.getMsg());
                        }
                    }
                });
    }

    @Override
    public void calCarts(String ids) {
        mModel.calCarts(ids)
                .subscribe(new SubscriberImpl<BaseResult<String>>() {
                    @Override
                    public void onError(Throwable e) {
//                        mView.hideProgress();
//                        ToastUtils.showShort("");
                        ToastUtils.showShort("网络错误");
                    }

                    @Override
                    public void onNext(BaseResult<String> stringBaseResult) {
                        if (stringBaseResult.isSuccess()) {
                            mView.setString(stringBaseResult.getData());
                        }
                    }
                });
    }
}
